jQuery( document ).ready(function() {
    
	function activateEvent() {
	   jQuery('.ivoter').length && jQuery('.ivoter').bind('click', function() {
	        var that = jQuery( this );
			var url = jQuery(this).attr('rel');
			if (url) {
				jQuery.ajax({
					url: url,
					timeout:  5000,
					success: function(response) {
       	                var reviewPopup =  jQuery(response).css({'position':'absolute'}).appendTo(document.body);
                        /*
						reviewPopup.popup({
            				hidePopup: true,
            				close: function(){
            					//jQuery('#formLogin')[0].reset();
                                //alert('hello world :)');
                                that.attr('rel', 'hello world');
            				}
            			});
                        */
                        reviewPopup.popup();
                        
                        reviewPopup.find('.auto-submit-star').rating({
                            callback: function(value, link) {
                            jQuery.ajax({
                                    url: jQuery(this).closest('form').attr('action'),
                                    type: jQuery(this).closest('form').attr('method'),
                                    data: jQuery(this).closest('form').serialize(),
                                    success: function(data) {
                                        //$('#response').html(data);
                                        var rate = parseInt(data);
                                        if (rate >= 100) {
                                            that.prev().addClass('star-rating-on');
                                            that.prev().prev().addClass('star-rating-on');
                                            that.prev().prev().prev().addClass('star-rating-on');
                                            that.prev().prev().prev().prev().addClass('star-rating-on');
                                            that.prev().prev().prev().prev().prev().addClass('star-rating-on');
                                        } else if (rate >= 80) {
                                            that.prev().removeClass('star-rating-on');
                                            that.prev().prev().addClass('star-rating-on');
                                            that.prev().prev().prev().addClass('star-rating-on');
                                            that.prev().prev().prev().prev().addClass('star-rating-on');
                                            that.prev().prev().prev().prev().prev().addClass('star-rating-on');
                                        } else if (rate >= 60) {
                                            that.prev().removeClass('star-rating-on');
                                            that.prev().prev().removeClass('star-rating-on');
                                            that.prev().prev().prev().addClass('star-rating-on');
                                            that.prev().prev().prev().prev().addClass('star-rating-on');
                                            that.prev().prev().prev().prev().prev().addClass('star-rating-on');
                                        } else if (rate >= 40) {
                                            that.prev().removeClass('star-rating-on');
                                            that.prev().prev().removeClass('star-rating-on');
                                            that.prev().prev().prev().removeClass('star-rating-on');
                                            that.prev().prev().prev().prev().addClass('star-rating-on');
                                            that.prev().prev().prev().prev().prev().addClass('star-rating-on');
                                        } else if (rate >= 20) {
                                            that.prev().removeClass('star-rating-on');
                                            that.prev().prev().removeClass('star-rating-on');
                                            that.prev().prev().prev().removeClass('star-rating-on');
                                            that.prev().prev().prev().prev().removeClass('star-rating-on');
                                            that.prev().prev().prev().prev().prev().addClass('star-rating-on');
                                        }
                                        
                                        
                                    }
                                });
       
                            }
                    });
                        
					},
					error: function () {
						alert(url + ' is not valid');
					}
				})
			}
		});
	}

	activateEvent();

});